<template>
  <div>
    <!--页面区域-->
    <div class="page-view">
      <a-table
        size="middle"
        rowKey="id"
        :loading="data.loading"
        :columns="columns"
        :data-source="data.dataList"
        :scroll="{ x: 'max-content' }"
        :pagination="{
          size: 'default',
          current: data.page,
          pageSize: data.pageSize,
          onChange: (current) => (data.page = current),
          showSizeChanger: false,
          showTotal: (total) => `共${total}条数据`,
        }"
      >
        <template #bodyCell="{ record, column }">
          <template v-if="column.key === 'operation'">
            <span>
              <a @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical" />
              <a-popconfirm title="确定删除?" ok-text="是" cancel-text="否" @confirm="confirmDelete(record)">
                <a href="#">删除</a>
              </a-popconfirm>
            </span>
          </template>
        </template>
      </a-table>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { FormInstance, message } from 'ant-design-vue';
  import { listOpLogListApi } from '/@/api/log';
  import { ref, reactive, onMounted } from 'vue';

  interface DataItem {
    index: number;
    id: string | number;
    re_method: string;
    re_url: string;
    re_ip: string;
    re_time: string;
    access_time: number;
  }

  const columns = reactive([
    {
      title: '序号',
      dataIndex: 'index',
      key: 'index',
      align: 'center',
    },
    {
      title: '请求方式',
      dataIndex: 're_method',
      key: 're_method',
      align: 'center',
    },
    {
      title: '请求URL',
      dataIndex: 're_url',
      key: 're_url',
      align: 'center',
    },
    {
      title: '操作IP',
      dataIndex: 're_ip',
      key: 're_ip',
      align: 'center',
    },
    {
      title: '操作时间',
      dataIndex: 're_time',
      key: 're_time',
      align: 'center',
    },
    {
      title: '耗时',
      dataIndex: 'access_time',
      key: 'access_time',
      align: 'center',
      customRender: ({ text, record, index, column }) => text + 'ms',
    },
  ]);

  // 页面数据
  const data = reactive({
    dataList: [] as DataItem[],
    loading: false,
    keyword: '',
    selectedRowKeys: [] as (string | number)[],
    pageSize: 10,
    page: 1,
  });

  onMounted(() => {
    getDataList();
  });

  const getDataList = () => {
    data.loading = true;
    listOpLogListApi({
      keyword: data.keyword,
    })
      .then((res) => {
        data.loading = false;
        console.log(res);
        res.data.forEach((item: DataItem, index: number) => {
          item.index = index + 1;
        });
        data.dataList = res.data;
      })
      .catch((err) => {
        data.loading = false;
        console.log(err);
      });
  };

  // 定义 handleEdit 函数
  const handleEdit = (record: DataItem) => {
    console.log('编辑记录:', record);
    // 这里可以添加编辑逻辑，比如打开一个编辑模态框
  };

  // 定义 confirmDelete 函数
  const confirmDelete = (record: DataItem) => {
    console.log('删除记录:', record);
    // 这里可以添加删除逻辑，比如调用删除接口
    message.success('删除成功');
    getDataList(); // 删除后刷新数据
  };
</script>

<style scoped lang="less">
  .page-view {
    min-height: 100%;
    background: #fff;
    padding: 24px;
    display: flex;
    flex-direction: column;
  }

  .table-operations {
    margin-bottom: 16px;
    text-align: right;
  }

  .table-operations > button {
    margin-right: 8px;
  }
</style>
